<?php

class Post
{
	private $id='';
	private $title='';
	private $body='';
	private $date='';
	private $catid='';
	private $catname='';
	private $commenting_status='';
	
	//komentarji
	private $comments = array();
	private $numComments;
	
	function Post()
	{
		
	}
	
	function setData($data)
	{
		$this->id = $data['id'];//itak, da rabmo - linki na posamezne poste
		$this->title = $data['title'];
		$this->body =$data['body'];
		$this->date =$data['date'];
		$this->catid =$data['catid'];
		$this->commenting_status = $data['commenting'];
	}
	
	public function getTitle()
	{
		return $this->title;
	}
	
	public function getBody()
	{
		return $this->body;
	}
	
	public function getCatid()
	{
		return $this->catid;
	}
	
	function getComments()
	{
		//select comments, for this post id
		$query = "SELECT * FROM comments WHERE parentid = '$this->id'";

		//rezultati iskanja
		$result = getFullAssoc($query);
		
		//ven dobi asociativno polje
		foreach ($result as $re)
		{
			//za vsakega naredi novo instanco objave in jo nafilej s podatki ter shrani na polje objav
			$pos = new Comment();
			$pos->setData($re);
			$this->comments[] = $pos;
		}
	}
	
	public function getCategory()
	{		
		if($this->catid <= 0)
		{
			$this->catname = 'no category';
			return;
		}
		
		//select comments, for this post id
		$query = "SELECT name FROM category WHERE id=$this->catid";

		$re  = getAssoc($query);
	
		$this->catname = $re['name'];
	}
	
	function getLatestPosts() 
	{
		$out = array();
		
		$query ="SELECT id,title,body,date,catid FROM posts ORDER BY date DESC LIMIT 5";

		//rezultati iskanja
		$result = getFullAssoc($query);

		//naredi objavo
		foreach ($result as $row) 
		{			
			$pos = new Post();
			$pos->setData($row);
			$pos->getCategory();
			
			$out[] = $pos;
		}

		return $out;
	}
	
	
	function countAll() 
	{
		$query ="SELECT Count(*) FROM posts";
		
		$re = getRow($query);
		
		return $re[0];
	}
	
	function getNumComments()
	{
		//select comments, for this post id
		$query ="SELECT Count(*) FROM comments WHERE parentid='$this->id' AND flag = '0'";
		
		$re = getRow($query);
		
		$this->numComments = $re[0];
	}
	
	//post z IDjem
	function getPost($postid)
	{
		$query ="SELECT id,title,body,date,catid,commenting FROM posts WHERE id = $postid";

		//naredi objavo
		$row = getAssoc($query);

		$pos = new Post();
		$pos->setData($row);
		$pos->getCategory();
		$pos->getNumComments();
		$pos->getComments();

		return $pos;
	}
	
	public function isPublished($id) 
	{
		$query ="SELECT state FROM posts WHERE id='$id'";
		
		$re = getRow($query);
		
		if($re[0] === '0')
			return True;
		else
			return False;
	}
	
	function commentsAllowed($postid) 
	{
		$query ="SELECT commenting FROM posts WHERE id='$postid'";
		
		$re = getRow($query);
		
		if($re[0] === '0')
			return True;
		else
			return False;
	}
	
	
	public function displayPostOnly()
	{
		$this->displayPost();
		
		//print_r($this->comments);
		
		$temp = new template();
		$temp->load($GLOBALS['prefs']['theme']."/comments.php");
		
		//{comments_num}
		$temp->assign("comments_num",$this->numComments);
		
		//{blog_url} -> '.WEBSITE_URL.'index.php
		$temp->assign("blog_url",WEBSITE_URL."index.php");
		
		//{post_id} -> '.$this->id.'
		$temp->assign("post_id",$this->id);
								
		//{post_title}
		$temp->assign("post_title",$this->title);
		
		//{akismet} and {captcha}
		if($GLOBALS['prefs']['use_akismet'])
		{		
			$temp->assign("akismet","<p><small><strong>Please note:</strong> Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.</small></p>");
		}
		
		if($GLOBALS['prefs']['use_captcha'])
		{		
			$temp->assign("captcha","<img src=\"".WEBSITE_URL."captcha/view.php\"><br />
			
			<label>Enter the string of characters appearing in the picture:</label>
			<input type=\"text\" name=\"key\" /><br />");
		}

		$temp->assign("closed",$this->commenting_status);
			
		//assign comments array
		$temp->assign("array",$this->comments);
		
		$temp->render();
	}
	
	public function displayPost()
	{
		//load post template
		$temp = new template();
		$temp->load($GLOBALS['prefs']['theme']."/post.php");
		
		//new
		
		//"An image in a post" -> 
		//http://themes.wordpress.net/testrun/?p=9 -> 
		//http://themes.wordpress.net/testrun/?p=9#comments -> {comments_link}
		//5 -> {comments_num}
		//{cat_name}
		//{cat_link}
		//{text}
	
		//post-9 -> {post_id}
		$temp->assign("post_id",$this->id);
		
		//{post_link}
		$temp->assign("post_link",WEBSITE_URL."index.php?action=post&amp;postid=".$this->id);
		
		//{post_title}
		$temp->assign("post_title",$this->title);
		$temp->assign("timestamp",date('F jS \of Y',strtotime($this->date)));
		$temp->assign("text",$this->body);
		
		//comments
		
		//{comment_link}
		$temp->assign("comments_link",WEBSITE_URL."index.php?action=post&amp;postid=".$this->id."#comments");
		//{lang_comments} 
		$temp->assign("lang_comments","Comments");
		//({comment_num})
		$temp->assign("comments_num",$this->numComments);
		
		//category
		$temp->assign("cat_link",WEBSITE_URL."index.php?action=category&amp;pageid=1&amp;catid=".$this->catid);
		$temp->assign("cat_name",$this->catname);
		
		$temp->render();
	}
	
	function displayPostInEdit()
	{
		echo '
		<div class="comments-template">
			<ol class="commentlist">
	
				<li>
			
					<h3>
						<a href="'.WEBSITE_URL.'index.php?action=admin&amp;post=edit&amp;postid='.$this->id.'" title="Edit '.$this->title.'">'.$this->title.'</a>
					</h3> 
		
					<b>'.date('F jS \of Y',strtotime($this->date)).'</b> | 
					
					<a href="'.WEBSITE_URL.'index.php?action=admin&amp;post=edit&amp;postid='.$this->id.'" rel="bookmark" title="Edit '.$this->title.'">(edit)</a> | <a href="'.WEBSITE_URL.'index.php?action=admin&amp;post=delete&amp;postid='.$this->id.'" rel="bookmark" title="Delete '.$this->title.'">(delete)</a>				
				</li>
				
			</ol>
		</div>	
		';
	}
	
	function displayDraftInEdit()
	{
		echo '
		<div class="comments-template">
			<ol class="commentlist">
	
				<li>
			
					<h3>
						<a href="'.WEBSITE_URL.'index.php?action=admin&amp;post=edit_draft&amp;postid='.$this->id.'" title="Edit '.$this->title.'">'.$this->title.'</a>
					</h3> 
		
					<b>'.date('F jS \of Y',strtotime($this->date)).'</b> | 
					
					<a href="'.WEBSITE_URL.'index.php?action=admin&amp;post=edit_draft&amp;postid='.$this->id.'" rel="bookmark" title="Edit '.$this->title.'">(edit)</a> | <a href="'.WEBSITE_URL.'index.php?action=admin&amp;post=delete&amp;postid='.$this->id.'" rel="bookmark" title="Delete '.$this->title.'">(delete)</a>				
				</li>
				
			</ol>
		</div>	
		';
	}
	
	function displayPostInInfo() 
	{	
		echo '
		<div class="comments-template">
			<ol class="commentlist">
	
				<li>
					<h3 id="'.$this->id.'"><a href="'.WEBSITE_URL.'index.php?action=admin&amp;post=edit&amp;postid='.$this->id.'" rel="bookmark" title="Permanent Link to '.$this->title.'">'.$this->title.'</a></h3>
		
					<p class="small">'.date('l F jS \, Y',strtotime($this->date)).'</p>
									
				</li>
				
			</ol>
		</div>';
	}
	
	function addPost()
	{
		$title =  $_REQUEST['title'];
		$body =  $_REQUEST['body'];
		$catid =  $_REQUEST['catid'];
		
		if(isset($_REQUEST['draft']))
		//create sql query
		$sql = "INSERT INTO `posts` (`title`, `body`, date, `catid`,`state`) VALUES ('$title', '$body',CURDATE(),'$catid','1');";
			else
		$sql = "INSERT INTO `posts` (`title`, `body`, date, `catid`,`state`) VALUES ('$title', '$body',CURDATE(),'$catid','0');";
		
		$result = query($sql);
		
		if(isset($_REQUEST['draft']))
			echo 'Post added as draft succesfully!';
		else
			echo 'Post added succesfully!';
	}
	
	function updatePost()
	{
		$postid =  $_REQUEST['postid'];
		$title =  $_REQUEST['title'];
		$body =  $_REQUEST['body'];
		$catid =  $_REQUEST['catid'];
		
		if(isset($_REQUEST['draft']))
		//create sql query
		{
			$sql = "UPDATE posts SET title = '$title', body = '$body', catid = '$catid',state = '1' WHERE posts.id = $postid LIMIT 1;";
		}
		else	
			$sql = "UPDATE posts SET title = '$title', body = '$body', catid = '$catid' WHERE posts.id = $postid LIMIT 1;";  
		
		$result = query($sql);
		
		if(isset($_REQUEST['draft']))
			echo 'Draft posted succesfully!';
		else
			echo 'Post updated succesfully!';
	}
	
	function deletePost() 
	{
		$postid =  $_REQUEST['postid'];
		
		$sql = "DELETE FROM posts WHERE id=$postid";
		
		$result = query($sql);
		
		echo 'Post succesfully deleted!';
	}
	
}

?>